package com.sdleyou.treepro.common.service.impl;

import com.sdleyou.treepro.common.exception.ServiceException;
import com.sdleyou.treepro.common.model.convert.CompanyDO2CompanyVOConverter;
import com.sdleyou.treepro.common.model.dao.CompanyDO;
import com.sdleyou.treepro.common.model.dao.Page;
import com.sdleyou.treepro.common.model.dao.UserDO;
import com.sdleyou.treepro.common.model.enums.ErrorCodeEnum;
import com.sdleyou.treepro.common.model.vo.CompanyVO;
import com.sdleyou.treepro.common.service.CompanyManager;
import com.sdleyou.treepro.common.service.MongoPageManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class CompanyManagerImpl implements CompanyManager {
    @Autowired
    private MongoPageManager mongoPageManager;
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Page<CompanyVO> companyList(String status, Integer pageNum, Integer pageSize) {
        Query query = new Query();
        if (!status.equals("ALL")) {
            query = new Query(Criteria.where("status").is(status));
        }
        Page<CompanyDO> page = mongoPageManager.pageQuery(query, CompanyDO.class, pageSize, pageNum);
        List<CompanyDO> companyDOList = page.getData();
        List<CompanyVO> companyVOList = companyDOList.stream().map(e -> CompanyDO2CompanyVOConverter.convert(e)).collect(Collectors.toList());

        return new Page<>(page.getPageNo(), page.getDataTotal(), page.getPageSize(), companyVOList);
    }

    @Override
    public void additionStaff(String companyId) {
        Query query = new Query(Criteria.where("company_id").is(companyId));
        CompanyDO companyDO = mongoTemplate.findOne(query, CompanyDO.class);
        if (companyDO == null) {
            throw new ServiceException(ErrorCodeEnum.E013.code(), "公司信息不存在");
        }
        Update update = new Update();
        update.set("member_count", companyDO.getMemberCount() + 1);
        mongoTemplate.updateFirst(query, update, CompanyDO.class);
        return;
    }

    @Override
    public void auditCompany(String companyId, String result) {
        Query query = new Query(Criteria.where("company_id").is(companyId));
        CompanyDO companyDO = mongoTemplate.findOne(query, CompanyDO.class);
        if (companyDO == null) {
            throw new ServiceException(ErrorCodeEnum.E013.code(), "公司信息不存在");
        }
        Update update = new Update();
        update.set("company_status", result);
        mongoTemplate.updateFirst(query, update, CompanyDO.class);

        Query userQuery = new Query(Criteria.where("user_id").is(companyDO.getLeaderId()));
        UserDO userDO = mongoTemplate.findOne(userQuery, UserDO.class);
        Update updateUser = new Update();
        updateUser.set("company_id", companyDO.getCompanyId());
        updateUser.set("role", "ADMIN");
        mongoTemplate.updateFirst(userQuery, updateUser, UserDO.class);

        return;
    }
}
